#include "system.h"
#include "bsp.h"
#include "drv_led.h"
#include "drv_beep.h"
#include "drv_key.h"
#include "drv_spi_w25qxx.h"
#include "drv_qspi_w25qxx.h"
#include "drv_ina3221.h"
#include "drv_sht30.h"
#include "drv_sdcard.h"
#include "drv_lan8720.h"



void qspi_test(uint32_t rand)
{
    uint32_t addr;
    uint32_t sector;
    int i;
    char buff[256];

    
    // addr = drv_qspi_w25qxx_get_size() - 4096;   //最后一扇区
    addr = 0x1000;
    sector = (addr / 4096) * 4096;

    printf("QSPI W25QXX Test (in 0x%X %d)\r\n", addr, sector);

    /* 写入 */
    memset(buff, 0x00, sizeof(buff));
    snprintf(buff, sizeof(buff)-1, "QSPI W25QXX test %d", rand);

    printf("[W]: %s\r\n", buff);
    drv_qspi_w25qxx_erase_sector(sector);
    drv_qspi_w25qxx_write(addr, (uint8_t*)buff, strlen(buff));

    /* 回读 */
    memset(buff, 0x00, sizeof(buff));

    drv_qspi_w25qxx_read(addr, (uint8_t*)buff, sizeof(buff)-1);
    printf("[R]: %s\r\n", buff);
    printf("[R-Hex]: ");
    for (i = 0; i < sizeof(buff); i++)
    {
        printf("%02X ", buff[i]);
    }
    printf("\r\n");

    /* 回读 addr=0x00000000 地址内容 */
    memset(buff, 0x00, sizeof(buff));
    addr = 0;
    drv_qspi_w25qxx_read(addr, (uint8_t*)buff, sizeof(buff)-1);
    printf("[0x%08X]:\r\n", addr);
    for (i = 0; i < sizeof(buff); i++)
    {
        printf("%02X ", buff[i]);
    }
    printf("\r\n");
}

void qspi_mapped(void)
{
    char *addr = (char *)0x90001000;
    int i;
    char buff[64];

    printf("QSPI W25QXX MemoryMapped\r\n");

    drv_qspi_w25qxx_memory_mapped();

    memset(buff, 0x00, sizeof(buff));
    memcpy(buff, addr, sizeof(buff));
    printf("[0x%08X]:\r\n", (uint32_t)addr);
    for (i = 0; i < sizeof(buff); i++)
    {
        printf("%02X ", buff[i]);
    }
    printf("\r\n");
}

void sdcard_info(void)
{
    drv_sdcard_info_obj info;
    drv_sdcard_cid_obj cid;

    uint64_t card_capacity; /* SD卡容量 */
    drv_sdcard_get_card_info(&info); //获取SD卡信息
    drv_sdcard_get_card_cid(&cid); //获取CID

    switch (info.card_type)
    {
    case CARD_SDSC:
    {
        if (info.card_version == CARD_V1_X)
        {
            printf("Card Type:SDSC V1\r\n");
        }
        else if (info.card_version == CARD_V2_X)
        {
            printf("Card Type:SDSC V2\r\n");
        }
    }
    break;

    case CARD_SDHC_SDXC:
        printf("Card Type:SDHC\r\n");
        break;
    }

    card_capacity = (uint64_t)(info.block_logical_nbr) * (uint64_t)(info.block_logical_size); /* 计算SD卡容量 */
    printf("Card ManufacturerID:%d\r\n", cid.manufacturer_id);                                /* 制造商ID */
    printf("Card RCA:%d\r\n", info.rel_card_addr);                       /* 卡相对地址 */
    printf("LogBlockNbr:%d \r\n", (uint32_t)(info.block_logical_nbr));   /* 显示逻辑块数量 */
    printf("LogBlockSize:%d \r\n", (uint32_t)(info.block_logical_size)); /* 显示逻辑块大小 */
    printf("Card Capacity:%d MB\r\n", (uint32_t)(card_capacity >> 20));  /* 显示容量 */
    printf("Card BlockSize:%d\r\n\r\n", info.block_size);                /* 显示块大小 */
}

int main(void)
{
	int i;
    int16_t ina_vol, ina_ele;
    float sht_tmp, sht_hum;

    bsp_init();

#ifdef __GNUC__
    printf("ARCH = GCC\r\n");
    printf("flash by OpenOCD\r\n");
#else
    printf("ARCH = ARMCC\r\n");
#endif

    drv_led_init();
    drv_beep_init();
    drv_key_init();

    drv_spi_w25qxx_init();
    drv_qspi_w25qxx_init();

    drv_ina3221_init();
    drv_sht30_init();

    drv_sdcard_init();
    if (drv_sdcard_is_exist())
    {
        printf("SD-Card detected\r\n");
        sdcard_info();
    }

    drv_lan8720_init();

    drv_beep_on();
    delay_ms(100);
    drv_beep_off();

    while (1)
    {
        // printf("STM32H750\r\n");
        
        drv_led_led0_on();
        drv_led_led1_off();
        delay_ms(500);
        drv_led_led0_off();
        drv_led_led1_on();
        delay_ms(500);

        if (drv_key_pres_key0())
        {
            printf("key0 press\r\n");
            // qspi_mapped();
            drv_qspi_w25qxx_erase_chip();
        }
        if (drv_key_pres_key1())
        {
            printf("key1 press\r\n");
            qspi_test(HAL_GetTick());
        }

        /* ina电压电流 */
		for (i = 2; i <= 3; i++)
		{
			drv_ina3221_get_value(i, &ina_vol, &ina_ele);
			printf("INA[%d], vol=%d.%03dmV, ele=%dmA; ", i, ina_vol/1000, ina_vol%1000, ina_ele);
		}
		printf("\r\n");
        
        /* sht32 温湿度 */
        drv_sht30_get_temp_hum(&sht_tmp, &sht_hum);
        printf("temperature = %.2f'C, humidity = %.2f%%\r\n", sht_tmp, sht_hum);


    }
    
}
